import {
	validate
} from './check'
/**
 * 将org 对象的属性值赋值给dst 属性名为一直的属性
 * @param {Object} org 旧对象
 * @param {Object} dst 新对象
 */
export function copyObject(org, dst) {
    for (let key in dst) {
      if (org.hasOwnProperty(key)) {
        dst[key] = org[key]
      }
    }
}
/**
 * 校验表单数据
 * @param {Object} dataObj 表单对象
 * @param {Object} dataConfig 校验配置
 */
export function checkFrom(dataObj, dataConfig) {
	 try {
	      // 循环配置（每个字段）
	      for (let key in dataConfig) {
	        //配置信息
	        let tmpDataConfigValue = dataConfig[key];
	        //对key进行处理
	        let keys = key.split(".");
	        // console.log("keys :", keys);
	        let tmpDataObj = dataObj;
	        //根据配置获取 数据值
	        keys.forEach(function (tmpKey) {
	          // console.log('tmpDataObj:', tmpDataObj);
	          tmpDataObj = tmpDataObj[tmpKey]
	        });
	        tmpDataConfigValue.forEach(function (configObj) {
	          if (configObj.limit == "required") {
	            validate.setState(validate.required(tmpDataObj), configObj.errInfo);
	          }
	          if (configObj.limit == 'phone') {
	            validate.setState(validate.phone(tmpDataObj), configObj.errInfo);
	          }
	          if (configObj.limit == 'email') {
	            validate.setState(validate.email(tmpDataObj), configObj.errInfo);
	          }
	          if (configObj.limit == 'maxin') {
	            let tmpParam = configObj.param.split(",")
	            validate.setState(validate.maxin(tmpDataObj, tmpParam[0], tmpParam[1]), configObj.errInfo);
	          }
	          if (configObj.limit == 'maxLength') {
	            validate.setState(validate.maxLength(tmpDataObj, configObj.param), configObj.errInfo);
	          }
	          if (configObj.limit == 'minLength') {
	            validate.setState(validate.minLength(tmpDataObj, configObj.param), configObj.errInfo);
	          }
	          if (configObj.limit == 'num') {
	            validate.setState(validate.num(tmpDataObj), configObj.errInfo);
	          }
	          if (configObj.limit == 'date') {
	            validate.setState(validate.date(tmpDataObj), configObj.errInfo);
	          }
	          if (configObj.limit == 'dateTime') {
	            validate.setState(validate.dateTime(tmpDataObj), configObj.errInfo);
	          }
	          if (configObj.limit == 'money') {
	            validate.setState(validate.money(tmpDataObj), configObj.errInfo);
	          }
	          if (configObj.limit == 'idCard') {
				  // 附加条件判断
				  if(configObj.additional !== undefined || configObj.additional !==null){
					  if(configObj.additional){
						validate.setState(validate.idCard(tmpDataObj), configObj.errInfo);
					  }else{
						  return true;
					  }
				  }else{
					validate.setState(validate.idCard(tmpDataObj), configObj.errInfo);
				  }
	          }
	        });
	
	      }
	    } catch (error) {
	      console.log("数据校验失败", validate.state, validate.errInfo, error);
		  	uni.showToast({
		  		title: validate.errInfo,
		  		icon: 'none',
		  		duration: 1500
		  	})
	      return false;
	    }
	    return true;
}
